load("//tools/build_defs/android:fb_xplat_cxx_library.bzl", "fb_xplat_cxx_library")
load("//tools/build_defs/oss:profilo_defs.bzl", "profilo_maybe_hidden_visibility", "profilo_path")
load(":build_defs.bzl", "unwindc_tracer_library")

unwindc_tracer_library("6.0.0")

unwindc_tracer_library("7.0.0")

unwindc_tracer_library("7.1.0")

unwindc_tracer_library("7.1.1")

unwindc_tracer_library("7.1.2")

PROFILER_EXPORTED_HEADERS = [
    "DalvikTracer.h",
    "SamplingProfiler.h",
]

fb_xplat_cxx_library(
    name = "constants",
    header_namespace = "profiler",
    exported_headers = [
        "Constants.h",
    ],
    visibility = [
        profilo_path("cpp/writer/..."),
    ],
)

fb_xplat_cxx_library(
    name = "artcompat",
    srcs = [
        "ArtCompatibility.cpp",
        "ArtCompatibilityRunner.cpp",
    ],
    headers = [
        "ArtCompatibility.h",
        "ArtCompatibilityRunner.h",
    ],
    header_namespace = "profiler",
    exported_headers = [
        "ArtCompatibility.h",
    ],
    allow_jni_merging = True,
    compiler_flags = [
        "-fvisibility=hidden",
        "-fexceptions",
        "-std=gnu++14",
        "-DLOG_TAG=\"Profilo/ArtCompatibility\"",
        #"-DFBLOG_NDEBUG=0",  # verbose logging
    ],
    force_static = True,
    soname = "libprofilo_stacktrace_artcompat.so",
    deps = [
        ":base_tracer",
        ":unwindc-tracer-6.0.0",
        ":unwindc-tracer-7.0.0",
        ":unwindc-tracer-7.1.0",
        ":unwindc-tracer-7.1.1",
        ":unwindc-tracer-7.1.2",
        profilo_path("deps/fb:fb"),
        profilo_path("deps/fbjni:fbjni"),
        profilo_path("deps/forkjail:forkjail"),
        profilo_path("cpp/logger:logger"),
    ],
)

fb_xplat_cxx_library(
    name = "base_tracer",
    header_namespace = "profiler",
    exported_headers = [
        "BaseTracer.h",
    ],
    visibility = [
        "PUBLIC",
    ],
)

fb_xplat_cxx_library(
    name = "external_tracer",
    srcs = [
        "ExternalTracer.cpp",
    ],
    header_namespace = "profiler",
    exported_headers = [
        "ExternalTracer.h",
    ],
    force_static = True,
    visibility = [
        "PUBLIC",
    ],
    deps = [
        ":base_tracer",
        profilo_path("cpp/api:external_api"),
    ],
)

fb_xplat_cxx_library(
    name = "external_tracer_manager",
    srcs = [
        "ExternalTracerManager.cpp",
    ],
    header_namespace = "profiler",
    exported_headers = [
        "ExternalTracerManager.h",
    ],
    force_static = True,
    visibility = [
        "PUBLIC",
    ],
    deps = [
        ":external_tracer",
        profilo_path("cpp/api:external_api"),
    ],
)

fb_xplat_cxx_library(
    name = "js_tracer",
    srcs = [
        "JSTracer.cpp",
    ],
    header_namespace = "profiler",
    exported_headers = [
        "JSTracer.h",
    ],
    force_static = True,
    preprocessor_flags = [
        "-DLOG_TAG=\"Profilo/JS\"",
    ],
    visibility = [
        "PUBLIC",
    ],
    deps = [
        ":external_tracer",
        profilo_path("deps/atomic:atomic"),
        profilo_path("cpp/logger:logger"),
    ],
)

fb_xplat_cxx_library(
    name = "tracer_utils",
    srcs = [
        "DalvikUtils.cpp",
    ],
    header_namespace = "profiler",
    exported_headers = [
        "DalvikUtils.h",
    ],
    force_static = True,
    visibility = [
        "PUBLIC",
    ],
    deps = [
        profilo_path("deps/dalvik:dalvik-subset-headers"),
    ],
)

fb_xplat_cxx_library(
    name = "profiler",
    srcs = [
        "DalvikTracer.cpp",
        "SamplingProfiler.cpp",
        "jni.cpp",
    ],
    headers = glob(
        ["*.h"],
        exclude = PROFILER_EXPORTED_HEADERS,
    ),
    header_namespace = "profiler",
    exported_headers = PROFILER_EXPORTED_HEADERS,
    allow_jni_merging = True,
    compiler_flags = [
        "-fexceptions",
        "-DLOG_TAG=\"Profilo/Profiler\"",
        "-Wno-format-security",
        "-Wno-unknown-attributes",
        #'-DFBLOG_NDEBUG=0', # verbose logging
        "-std=gnu++14",
    ] + profilo_maybe_hidden_visibility(),
    soname = "libprofilo_stacktrace.$(ext)",
    supported_platforms_regex = "^android-(armv7|x86)$",
    visibility = [
        "PUBLIC",
    ],
    deps = [
        ":artcompat",
        ":base_tracer",
        ":constants",
        ":external_tracer",
        ":external_tracer_manager",
        ":js_tracer",
        ":tracer_utils",
        ":unwindc-tracer-6.0.0",
        ":unwindc-tracer-7.0.0",
        ":unwindc-tracer-7.1.0",
        ":unwindc-tracer-7.1.1",
        ":unwindc-tracer-7.1.2",
        profilo_path("cpp:constants"),
        profilo_path("cpp:profilo"),
        profilo_path("cpp:providers"),
        profilo_path("cpp/logger:logger"),
        profilo_path("cpp/sigmuxsetup:sigmuxsetup"),
        profilo_path("cpp/util:util"),
        profilo_path("deps/dalvik:dalvik-subset-headers"),
        profilo_path("deps/fb:fb"),
        profilo_path("deps/fbjni:fbjni"),
        profilo_path("deps/sigmux:sigmux"),
        profilo_path("cpp/api:external_api"),
    ],
)
